package com.taomu.app.zongchouhuli.framework;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import com.taomu.app.zongchouhuli.utils.LoadingDialog;
import com.taomu.app.zongchouhuli.utils.TokenDialog;

public abstract class BaseActivity<T extends BasePresenter> extends AppCompatActivity implements UsualViewOperator{

    private Dialog mDialog;

    private int showDialog = 0;

    protected T mPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //隐藏标题栏
        getSupportActionBar().hide();
        setContentView(getContentLayout());
        initProgressDialog();
        mPresenter = createPresenter();
        initView();
        initData();
        initEvent();
    }

    private void initProgressDialog(){
        mDialog = LoadingDialog.createLoadingDialog(this, "加载中...");
    }

    @Override
    public void showToast(String msg) {
        Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();
    }

    protected void startActivity(Class clazz){
        Intent intent = new Intent(this,clazz);
        startActivity(intent);
    }

    protected void startActivityWithResults(Class clazz, int requestCode){
        Intent intent = new Intent(this,clazz);
        startActivityForResult(intent,requestCode);
    }

    protected void startActivityForResultsWithData(Class clazz, int requestCode, String key,String value){
        Intent intent = new Intent(this,clazz);
        intent.putExtra(key,value);
        startActivityForResult(intent,requestCode);
    }

    protected abstract int getContentLayout();

    protected abstract void initView();

    protected abstract void initData();

    protected abstract void initEvent();

    protected abstract T createPresenter();

    @Override
    public void showLoadingDialog() {
        if(mDialog.isShowing()){
            showDialog++;
        }else{
            showDialog = 1;
            mDialog.show();
        }
    }

    @Override
    public void dismissLoadingDialog() {
        showDialog--;
        if(showDialog<=0){
            if(mDialog != null){
                LoadingDialog.closeDialog(mDialog);
            }
        }
    }

    @Override
    public void onToLogin() {

    }

    @Override
    public void showToken() {
        TokenDialog.getInsence(this).show();
    }
}